package com.hessian.test.service;

import com.google.gson.Gson;
import com.ibetter.test.dao.UserDao;
import com.ibetter.test.domain.UserBase;
import com.light.hessian.exporter.annotation.HessianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;

/**
 * Created by zhaojun on 2017/04/27.
 */
@HessianService
public class HelloServiceImpl implements HelloService{

    @Autowired
    private UserDao userDao;

    @Autowired
    RedisTemplate redisTemplate;

    @Override

    @Transactional
    public String hello(String message) {
        Gson gson=new Gson();
        UserBase userInfo = userDao.getUserBaseById(395);
        System.out.println(gson.toJson(userInfo));

        redisTemplate.execute(new RedisCallback() {
            @Override
            public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {

                try {
                    byte[] rtn = redisConnection.hGet("mmap:userinfo:300".getBytes("utf-8"), "name".getBytes("utf-8"));
                    System.out.println(new String(rtn,"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });

        userDao.deleteById(1);
        if (0<1){
            throw new RuntimeException("sasasa");
        }
        return message;
    }
}
